/**
 * 高度量算
 */
import { handler } from './mapClear'
import { removeInputAction } from './measureArea'
let hyp = null
// let singleLine = true //显示单条等高线
const contourDistance = 10 // 等高距

export default () => {
  const viewer = window.viewer
  const layers = window.scene.layers.layerQueue

  const Cesium = window.Cesium

  if (!viewer || !Cesium) {
    return
  }
  removeInputAction()
  if (handler.measureArea) {
    handler.measureArea.deactivate()
  }
  if (handler.measureDistance) {
    handler.measureDistance.deactivate()
  }
  if (handler.measureHeight) {
    handler.measureHeight.clear()
    handler.measureHeight.activate()
    resetLine()
    return handler
  }
  handler.measureHeight = new Cesium.MeasureHandler(
    viewer,
    Cesium.MeasureMode.DVH,
    0
  )
  handler.measureHeight.measureEvt.addEventListener(height)
  // 激活事件，监听当前事件以获取处理器的状态
  handler.measureHeight.activeEvt.addEventListener(activeFun)
  handler.measureHeight.activate()

  // 创建等值线
  createIsoline()

  /**
   *
   * @param {*} result
   */
  function height(result) {
    const distance =
      result.distance > 1000
        ? (result.distance / 1000).toFixed(2) + 'km'
        : result.distance + 'm'
    const vHeight =
      result.verticalHeight > 1000
        ? (result.verticalHeight / 1000).toFixed(2) + 'km'
        : result.verticalHeight + 'm'
    const hDistance =
      result.horizontalDistance > 1000
        ? (result.horizontalDistance / 1000).toFixed(2) + 'km'
        : result.horizontalDistance + 'm'
    handler.measureHeight.disLabel.text = '空间距离:' + distance
    handler.measureHeight.vLabel.text = '垂直高度:' + vHeight
    handler.measureHeight.hLabel.text = '水平距离:' + hDistance
    // 关闭除了高度标签的其他标签
    handler.measureHeight.disLabel.show = false
    handler.measureHeight.hLabel.show = false

    // 获取高度值
    const position = result.directionalPositions[1]
    const cartographic = Cesium.Cartographic.fromCartesian(position)
    // var longitude = Cesium.Math.toDegrees(cartographic.longitude);
    // var latitude = Cesium.Math.toDegrees(cartographic.latitude);
    const height = cartographic.height - 5
    // height = scene.globe.getHeight(new Cesium.Cartographic.fromDegrees(longitude, latitude, 0)) + 3;

    // createIsoline(height, result.verticalHeight); //显示等高线

    // 实时修改等值线
    for (const layer of layers) {
      if (layer.hypsometricSetting) {
        layer.hypsometricSetting.hypsometricSetting.MaxVisibleValue = height
        layer.hypsometricSetting.hypsometricSetting.MinVisibleValue = height
      }
    }
  }

  /** 显示等值线
   *
   */
  function createIsoline() {
    if (!hyp) {
      hyp = new Cesium.HypsometricSetting()
    }
    hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.LINE
    // hyp.LineColor = Cesium.Color.YELLOW;
    hyp.LineColor = Cesium.Color.fromCssColorString('#ff9c00')

    // 等高线间隔为10m
    // hyp.LineInterval = 10.0;
    hyp.LineInterval = contourDistance
    // if (singleLine) {
    //   hyp.MinVisibleValue = height - contourDistance;
    //   hyp.MaxVisibleValue = height;
    // } else {
    //   hyp.MinVisibleValue = height;
    //   hyp.MaxVisibleValue = height + parseFloat(verticalHeight);
    // }

    // 设置分层设色的最大/最小可见高度
    hyp.MinVisibleValue = -1000
    hyp.MaxVisibleValue = -1000

    // 设置分层设色颜色表的最大/最小key值,表示在此高度范围内显示颜色表
    hyp.ColorTableMinKey = -1000
    hyp.ColorTableMaxKey = -1000

    // var colorTable = new Cesium.ColorTable();
    // colorTable.insert(1000, Cesium.Color.fromCssColorString("#f3b87c"));
    // colorTable.insert(0, Cesium.Color.fromCssColorString("#f3b87c"));
    // //设置分层设色的颜色表
    // hyp.ColorTable = colorTable;

    // 设置分层设色的透明度
    hyp.Opacity = 1

    // 设置地形等高线
    // viewer.scene.globe.HypsometricSetting = {
    //   hypsometricSetting: hyp,
    //   analysisMode: Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_ALL,
    // };

    for (const layer of layers) {
      layer.hypsometricSetting = {
        hypsometricSetting: hyp,
        analysisMode: Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_ALL
      }
    }
  }

  function activeFun(isActive) {
    if (isActive === true) {
      viewer.enableCursorStyle = false
      viewer._element.style.cursor = 'default'
    } else {
      viewer.enableCursorStyle = true
    }
  }
  return handler
}

/** 清除等高线
 *
 */
export function resetLine() {
  const viewer = window.viewer
  const layers = window.scene.layers.layerQueue

  viewer.scene.globe.HypsometricSetting = undefined

  for (const layer of layers) {
    // layer.hypsometricSetting = undefined //目前有缺陷
    if (layer.hypsometricSetting.hypsometricSetting) {
      layer.hypsometricSetting.hypsometricSetting.MinVisibleValue = -1000
      layer.hypsometricSetting.hypsometricSetting.MaxVisibleValue = -1000
    }
  }
}
